﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Web.Configuration;

namespace WebApplication1
{
    public partial class _return : System.Web.UI.Page
    {
        SqlConnection cnn = new SqlConnection(WebConfigurationManager.AppSettings["connect"]);
        string issid,accno;
        int fin,ret_no;
        DateTime exp;
        double dy;
        protected void Page_Load(object sender, EventArgs e)
        {
            issid = Request.QueryString["id"];
            cnn.Open();
            SqlCommand cmd = new SqlCommand("select * from cdissue where issno='" + issid + "'", cnn);
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                exp = Convert.ToDateTime(dr["expire"]);
                accno = dr["custno"].ToString();
            }
            dr.Close();
            SqlCommand cm = new SqlCommand("select top 1 rtno from cdreturn order by rtno desc", cnn);
            SqlDataReader dr1 = cm.ExecuteReader();
            if (dr1.HasRows == false)
                ret_no = 100;
            else
            {
                while (dr1.Read())
                {
                    ret_no = Convert.ToInt32(dr1["rtno"]) + 1;
                }
            }
            dr1.Close();
            DateTime tdy = DateTime.Today;
            int compare_val = tdy.CompareTo(exp);
            if (compare_val < 0)
            {
                SqlCommand cmd3 = new SqlCommand("insert into cdreturn values('"+ ret_no+"','"+issid+"','"+DateTime.Today+"')", cnn);
                cmd3.ExecuteNonQuery();
                SqlCommand cmd1 = new SqlCommand("update cdissue set active='false',status='Returned' where issno='" + issid + "'", cnn);
                cmd1.ExecuteNonQuery();
                SqlCommand cmd2 = new SqlCommand("update account set no_cd=no_cd-1 where accno='" + accno + "'", cnn);
                cmd2.ExecuteNonQuery();
                Response.Write("<script>alert('CD IS RETURNED')</script>");
                Response.Redirect("~/Admin/cdissue.aspx");
            }
            else if (compare_val > 0)
            {
                TimeSpan nody = tdy.Subtract(exp);
                dy = nody.TotalDays;
                fin = (int)dy * 5;
                SqlCommand cmd3 = new SqlCommand("insert into cdreturn values('" + ret_no + "','" + issid + "','" + DateTime.Today + "')", cnn);
                cmd3.ExecuteNonQuery();
                SqlCommand cmd2 = new SqlCommand("update cdissue set active='false',status='Returned',fine='" + fin + "' where issno='" + issid + "'", cnn);
                cmd2.ExecuteNonQuery();
                SqlCommand cmd1 = new SqlCommand("update account set no_cd=no_cd-1 where accno='" + accno + "'", cnn);
                cmd1.ExecuteNonQuery();
                Label1.Visible = true;
                Label2.Text = Convert.ToString(Convert.ToInt32(dy));
                Label2.Visible = true;
                Label3.Visible = true;
                Label4.Text = Convert.ToString(fin);
                Label4.Visible = true;
                Response.Write("<script>alert('CD IS RETURNED')</script>");
            }
            else if(compare_val==0)
            {
                SqlCommand cmd3 = new SqlCommand("insert into cdreturn values('" + ret_no + "','" + issid + "','" + DateTime.Today + "')", cnn);
                cmd3.ExecuteNonQuery();
                SqlCommand cmd1 = new SqlCommand("update cdissue set active='false',status='Returned' where issno='" + issid + "'", cnn);
                cmd1.ExecuteNonQuery();
                SqlCommand cmd2 = new SqlCommand("update account set no_cd=no_cd-1 where accno='" + accno + "'", cnn);
                cmd2.ExecuteNonQuery();
                Response.Redirect("~/Admin/cdissue.aspx");
            }
            cnn.Close();
        }

        protected void btn_paid_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Admin/cdissue.aspx");
        }
    }
}